package com.kyh.service.sys.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kyh.service.sys.ButtonService;
import com.kyh.sys.entity.Button;
import com.kyh.sys.entity.Menu;
import com.kyh.sys.mapper.ButtonMapper;
import com.kyh.sys.mapper.MenuMapper;
import com.kyh.sys.mapper.RoleButtonMapper;
import com.kyh.util.CheckUtil;
import com.kyh.vo.BootstrapTableVoJA;
import com.kyh.vo.R;

@Service("buttonService")
public class ButtonServiceImpl implements ButtonService {

	@Autowired
	private ButtonMapper buttonMapper;
	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private RoleButtonMapper roleButtonMapper;

	@Override
	@Transactional
	public R add(Button entity) throws Exception {
		
		Long menuId = entity.getMenuId();
		if (!CheckUtil.notNull(menuId)) {
			return R.error("菜单ID不能为空");
		}
		
		String buttonName = entity.getButtonName();
		if (!CheckUtil.notNull(buttonName)) {
			return R.error("名称不能为空");
		}
		
		String buttonAttr = entity.getButtonAttr();
		if (!CheckUtil.notNull(buttonAttr)) {
			return R.error("属性不能为空");
		}
		
		// 验证菜单
		Menu menuCheck = menuMapper.get(menuId);
		if (menuCheck == null) {
			return R.error("所属菜单不存在");
		}
		
		// 验证属性是否已存在
//		ConfButton conButton = new ConfButton();
//		conButton.e.setMenuId(menuId);
//		conButton.e.setButtonAttr(buttonAttr);
//		if (buttonMapper.count(conButton) > 0) {
		if (new Button().setMenuId(menuId).setButtonAttr(buttonAttr).count() > 0) {
			return R.error("该菜单下的此按钮属性已存在");
		}
		
		// 添加数据
		if (buttonMapper.insert(entity) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R edit(Button entity) throws Exception {
		
		Long buttonId = entity.getButtonId();
		if (!CheckUtil.notNull(buttonId)) {
			return R.error("ID不能为空");
		}

		Long menuId = entity.getMenuId();
		if (!CheckUtil.notNull(menuId)) {
			return R.error("菜单ID不能为空");
		}

		String buttonName = entity.getButtonName();
		if (!CheckUtil.notNull(buttonName)) {
			return R.error("名称不能为空");
		}

		String buttonAttr = entity.getButtonAttr();
		if (!CheckUtil.notNull(buttonAttr)) {
			return R.error("属性不能为空");
		}
		
		// 验证菜单
		Menu menuCheck = menuMapper.get(menuId);
		if (menuCheck == null) {
			return R.error("所属菜单不存在");
		}
		
		// 验证属性是否存在
		if (!buttonAttr.equals(entity.getButtonAttr())) {
//			ConfButton conButton = new ConfButton();
//			conButton.e.setMenuId(menuId);
//			conButton.e.setButtonAttr(buttonAttr);
			if (new Button().setMenuId(menuId).setButtonAttr(buttonAttr).count() > 0) {
				return R.error("该菜单下的此按钮属性已存在");
			}
		}
		
		// 更新数据
		if (buttonMapper.update(entity) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R deleteFalse(
			Long buttonId
			) throws Exception {
		
		if (buttonMapper.deleteFalse(buttonId) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R deleteTrue(Long buttonId) {
		
		buttonMapper.deleteTrue(buttonId);
		
		//  删除相关的授权
		roleButtonMapper.deleteTrueByButtonId(buttonId);
		
		return R.ok();
	}

	@Override
	public BootstrapTableVoJA search(Button cond) {
		
		List<Map<String, Object>> listMap = cond.queryMap();
		
		int total = cond.count();
		
        BootstrapTableVoJA vo = new BootstrapTableVoJA();
        vo.setRowsByListMap(listMap);
        vo.setTotal(total);
        
        return vo;
	}

	@Override
	public R tree() {

		JSONArray JAModule = new JSONArray();
//		List<Menu> listMenu = menuMapper.search(new ConfMenu());
		List<Menu> listMenu = new Menu().queryList();
		
		for (Menu menu : listMenu) {
			
//			ConfButton conButton = new ConfButton();;
//			conButton.orderBy.setOrderby("button_id");
//			conButton.orderBy.setSorttype("asc");
//			conButton.e.setMenuId(menu.getMenuId());
//			List<Button> listButton = buttonMapper.search(conButton);
			List<Button> listButton = new Button().setOrderby("A.button_id asc")
					.setMenuId(menu.getMenuId()).queryList();
        	JSONArray JAbutton = new JSONArray();
	        for (Button button : listButton) {
				JAbutton.add(button.tree());
	        }
	        
			JSONObject JOMenu = menu.tree();
			JOMenu.put("nodes", JAbutton);
			JAModule.add(JOMenu);
		}

        return R.ok(JAModule);
	}
}
